//
//  HomePageViewController.m
//  miaopai
//
//  Created by Crow Zhang on 15/11/12.
//  Copyright © 2015年 Crow Zhang. All rights reserved.
//

#import "HomePageViewController.h"
#import "HomePageDetailViewController.h"
#import "FlowWaterLayout.h"
#import "HomePageCollectionViewCell.h"
#import "SearchViewController.h"
#import <UINavigationBar+Awesome.h>

@interface HomePageViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, FlowWaterLayoutDelegate>

@property(nonatomic, assign)BOOL isPush;

@property(nonatomic, retain)UICollectionView *collectionView;

@property(nonatomic, retain)NSArray *nameArray;

@property(nonatomic, retain)NSArray *idArray;

@property(nonatomic, retain)NSArray *picArray;

@end

@implementation HomePageViewController

- (void)dealloc
{
    [_collectionView release];
    [_nameArray release];
    [_idArray release];
    [_picArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"首页";
    self.isPush = NO;
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"返回";
    self.navigationItem.backBarButtonItem = backItem;
    [backItem release];
    
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchButton setImage:[UIImage imageNamed:@"iconfont-sousuo1"] forState:UIControlStateNormal];
    searchButton.frame = CGRectMake(0, 0, 24, 24);
    [searchButton addTarget:self action:@selector(pushToSearchViewController) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:searchButton];

    self.navigationItem.rightBarButtonItem = rightItem;
    [rightItem release];
    
    self.nameArray = @[@"最新", @"明星", @"小咖秀", @"搞笑", @"女神", @"卖萌", @"韩娱"];
    self.idArray = @[@"1", @"62", @"110", @"68", @"67", @"107", @"106"];
    
    [self creatCollectionView];
    
}

- (void)creatCollectionView
{
    FlowWaterLayout *layout = [[FlowWaterLayout alloc] init];
    layout.delegate = self;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = [UIColor colorWithWhite:0.931 alpha:1.000];
    [_collectionView registerClass:[HomePageCollectionViewCell class] forCellWithReuseIdentifier:@"HomePageCollectionViewCell"];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    [layout release];
}

#pragma mark flowlayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0 && indexPath.section == 0) {
        return CGSizeMake(kWidth - 20, (kWidth - 40) / 3);
    }
    if (indexPath.section == 1 && (indexPath.item == 1 || indexPath.item == 4)) {
        return CGSizeMake((kWidth - 40) / 3 * 2 + 10, (kWidth - 40) / 3 * 2 + 10);
    }else
    {
        return CGSizeMake((kWidth - 40) / 3, (kWidth - 40) / 3);
    }
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else
    {
        return 6;
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomePageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomePageCollectionViewCell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        cell.text = [self.nameArray objectAtIndex:0];
        cell.imageName = @"zuixin.png";
    }else
    {
        cell.text = [self.nameArray objectAtIndex:indexPath.item + 1];
        if(indexPath.row == 0)
        {
            cell.imageName = @"mingxing.png";
        }
        if(indexPath.row == 1)
        {
            cell.imageName = @"xiaokaxiu.png";
        }
        if(indexPath.row == 2)
        {
            cell.imageName = @"gaoxiao.png";
        }
        if(indexPath.row == 3)
        {
            cell.imageName = @"nvsheng.png";
        }
        if(indexPath.row == 4)
        {
            cell.imageName = @"maimeng.png";
        }
        if(indexPath.row == 5)
        {
            cell.imageName = @"hangyu.png";
        }
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld %ld",indexPath.section , indexPath.item);
    HomePageDetailViewController *view = [[HomePageDetailViewController alloc] init];
    self.isPush = YES;
    if (indexPath.section == 0) {
        view.name = [self.nameArray objectAtIndex:0];
    }else
    {
        view.name = [self.nameArray objectAtIndex:indexPath.item + 1];
        view.scid = [self.idArray objectAtIndex:indexPath.item + 1];
    }
    [self.navigationController pushViewController:view animated:YES];
    [view release];
}

- (void)viewWillAppear:(BOOL)animated
{
    if (!self.isPush) {
        UIView * view = [self.tabBarController.view.subviews lastObject];
        [UIView animateWithDuration:0.5 animations:^{
            view.frame = CGRectMake(0, kHeight - 48, kWidth, 48);
        }];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    if (self.isPush) {
        self.isPush = NO;
    }

}

- (void)pushToSearchViewController
{
    SearchViewController *searchViewController = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchViewController animated:YES];

    [searchViewController release];
}

- (void)viewDidAppear:(BOOL)animated
{
    [self setNavigationBarTransformProgress:0];
}

- (void)setNavigationBarTransformProgress:(CGFloat)progress
{
    [self.navigationController.navigationBar lt_setTranslationY:(-44 * progress)];
    [self.navigationController.navigationBar lt_setElementsAlpha:(1-progress)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
